<!DOCTYPE html>
<title>Service Worker: Check script error sanitization</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/get-host-info.js"></script>
<script src="resources/test-helpers.js"></script>
<script>
var message_callback;
var host_info = get_host_info();

window.addEventListener('message', function(evt) {
    message_callback(evt.data);
  });

function get_script_error(frame, url) {
  return new Promise(function(resolve) {
      message_callback = resolve;
      frame.contentWindow.postMessage({url: url}, host_info['HTTP_ORIGIN']);
    });
}

function check_script_error(frame, url, should_be_sanitized) {
  if (should_be_sanitized) {
    return get_script_error(frame, url)
      .then(function(data) {
          assert_equals(data.filename, '');
          assert_equals(data.colno, 0);
          assert_equals(data.lineno, 0);
          assert_equals(data.message, 'Script error.');
        });
  } else {
    return get_script_error(frame, url)
      .then(function(data) {
          assert_equals(data.filename, url);
          assert_equals(data.colno, 1);
          assert_equals(data.lineno, 1);
          assert_equals(data.message,
                        'Uncaught ReferenceError: UndefinedReference is not defined');
        });
  }
}

async_test(function(t) {
    var SCOPE = 'resources/fetch-script-onerror-iframe.html';
    var WORKER_SCRIPT = 'resources/fetch-script-onerror-worker.js';
    var TARGET_PATH =
        '/serviceworker/resources/fetch-script-onerror.php';
    var SCRIPT_URL = host_info['HTTP_ORIGIN'] + TARGET_PATH;
    var REMOTE_SCRIPT_URL = host_info['HTTP_REMOTE_ORIGIN'] + TARGET_PATH;
    var frame;
    service_worker_unregister_and_register(t, WORKER_SCRIPT, SCOPE)
      .then(function(registration) {
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() { return with_iframe(SCOPE); })
      .then(function(f) {
          frame = f;
          return check_script_error(frame, SCRIPT_URL, false);
        })
      .then(function() {
          return check_script_error(frame, REMOTE_SCRIPT_URL, true);
        })
      .then(function() {
          return check_script_error(
              frame,
              host_info['HTTP_ORIGIN'] + '/dummy?mode=cors&url=' +
              encodeURIComponent(REMOTE_SCRIPT_URL),
              false);
        })
      .then(function() {
          return check_script_error(
              frame,
              host_info['HTTP_ORIGIN'] + '/dummy?mode=no-cors&url=' +
              encodeURIComponent(REMOTE_SCRIPT_URL),
              true);
        })
      .then(function() {
          return check_script_error(
              frame,
              host_info['HTTP_REMOTE_ORIGIN'] + '/dummy?mode=cors&url=' +
              encodeURIComponent(REMOTE_SCRIPT_URL),
              false);
        })
      .then(function() {
          return check_script_error(
              frame,
              host_info['HTTP_REMOTE_ORIGIN'] + '/dummy?mode=no-cors&url=' +
              encodeURIComponent(REMOTE_SCRIPT_URL),
              true);
        })
      .then(function() {
          frame.remove();
          service_worker_unregister_and_done(t, SCOPE);
        })
      .catch(unreached_rejection(t));
  }, 'Check script error sanitization');
</script>
